<template>
  <div class="login-container" :class="{ 'mobile-layout': isMobileDevice }">
    <div class="login-wrapper">
      <!-- 移动端Logo区域 -->
      <div v-if="isMobileDevice" class="mobile-logo">
        <div class="logo-icon">
          <el-icon size="48"><Monitor /></el-icon>
        </div>
        <h1>流量监控</h1>
        <p>智能流量管理助手</p>
      </div>

      <el-card class="login-card" :class="{ 'mobile-card': isMobileDevice }">
        <template #header v-if="!isMobileDevice">
          <h2>流量监控系统</h2>
          <p>请登录您的账户</p>
        </template>
        
        <div v-if="isMobileDevice" class="mobile-form-header">
          <h3>账户登录</h3>
        </div>

        <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="loginRules"
          :size="isMobileDevice ? 'default' : 'large'"
          :label-position="isMobileDevice ? 'top' : 'right'"
          class="login-form"
        >
          <FormField
            type="input"
            prop="username"
            v-model="loginForm.username"
            placeholder="请输入用户名"
            :rules="loginRules.username"
          />
          
          <FormField
            type="input"
            input-type="password"
            prop="password"
            v-model="loginForm.password"
            placeholder="请输入密码"
            :rules="loginRules.password"
          />

          <el-form-item class="login-actions">
            <el-button
              type="primary"
              :class="{ 'mobile-login-btn': isMobileDevice }"
              style="width: 100%"
              :loading="loading"
              @click="handleLogin"
            >
              登录
            </el-button>
          </el-form-item>

          <el-form-item class="register-link">
            <el-button
              type="text"
              style="width: 100%"
              @click="$router.push('/register')"
              :class="{ 'mobile-text-btn': isMobileDevice }"
            >
              还没有账户？立即注册
            </el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Monitor } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user'
import FormField from '@/components/FormField.vue'

const router = useRouter()
const userStore = useUserStore()

const loginFormRef = ref()
const loading = ref(false)

// 设备类型检测
const isMobileDevice = computed(() => {
  return window.innerWidth <= 768 || document.body.classList.contains('mobile-device')
})

const isDesktopDevice = computed(() => {
  return window.innerWidth > 768 || document.body.classList.contains('desktop-device', 'tablet-device')
})

const loginForm = reactive({
  username: '',
  password: ''
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, message: '用户名至少3位', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码至少6位', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  try {
    await loginFormRef.value.validate()
    loading.value = true
    
    await userStore.login(loginForm)
    
    ElMessage.success('登录成功')
    router.push('/traffic-monitor')
  } catch (error) {
    console.error('登录失败:', error)
    ElMessage.error(error.message || '登录失败')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.login-container.mobile-layout {
  align-items: flex-start;
  padding: 0;
  background: #f8f9fa;
}

.login-wrapper {
  width: 100%;
  max-width: 400px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* 移动端Logo区域 */
.mobile-logo {
  text-align: center;
  padding: 40px 20px 30px 20px;
  color: white;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  width: 100%;
  border-radius: 0 0 24px 24px;
  margin-bottom: 20px;
}

.logo-icon {
  margin-bottom: 16px;
}

.mobile-logo h1 {
  margin: 0 0 8px 0;
  font-size: 24px;
  font-weight: 600;
  color: white;
}

.mobile-logo p {
  margin: 0;
  font-size: 14px;
  color: rgba(255, 255, 255, 0.8);
}

/* 登录卡片 */
.login-card {
  width: 100%;
  max-width: 400px;
  border-radius: 8px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.login-card.mobile-card {
  margin: 0 16px;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.login-card :deep(.el-card__header) {
  text-align: center;
  background: #f8f9fa;
  border-radius: 8px 8px 0 0;
}

.login-card h2 {
  margin: 0 0 8px 0;
  color: #2c3e50;
}

.login-card p {
  margin: 0;
  color: #6c757d;
  font-size: 14px;
}

/* 移动端表单头部 */
.mobile-form-header {
  text-align: center;
  padding-bottom: 20px;
  border-bottom: 1px solid #eee;
  margin-bottom: 24px;
}

.mobile-form-header h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: #333;
}

/* 表单样式 */
.login-form {
  padding: 0 4px;
}

.login-actions {
  margin-top: 24px;
}

.mobile-login-btn {
  height: 48px !important;
  font-size: 16px !important;
  border-radius: 8px !important;
  font-weight: 600;
}

.register-link {
  margin-top: 16px;
  margin-bottom: 0;
}

.mobile-text-btn {
  color: #667eea !important;
  font-size: 14px;
}

/* 移动端优化 */
@media (max-width: 480px) {
  .login-container.mobile-layout {
    padding: 0;
  }
  
  .mobile-logo {
    padding: 30px 20px 24px 20px;
    border-radius: 0 0 20px 20px;
  }
  
  .mobile-logo h1 {
    font-size: 20px;
  }
  
  .login-card.mobile-card {
    margin: 0 12px;
  }
  
  .mobile-form-header h3 {
    font-size: 16px;
  }
}

/* 键盘弹起时的适配 */
@media (max-height: 600px) {
  .mobile-logo {
    padding: 20px 20px 16px 20px;
  }
  
  .mobile-logo h1 {
    font-size: 18px;
  }
  
  .mobile-logo p {
    font-size: 12px;
  }
}

/* 横屏适配 */
@media (max-width: 768px) and (orientation: landscape) {
  .login-container.mobile-layout {
    flex-direction: row;
    align-items: center;
    justify-content: center;
    padding: 20px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  }
  
  .mobile-logo {
    display: none;
  }
  
  .login-card.mobile-card {
    margin: 0;
    max-width: 350px;
  }
}
</style>
